<?php
$AppUI->savePlace();

// pull all the key types
$perms =& $AppUI->acl();

// Get the permissions for this module
$canAccess = $perms->checkModule('roles', 'access');
if (! $canAccess) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
$canRead = $perms->checkModule('roles', 'view');
$canAdd = $perms->checkModule('roles', 'add');
$canEdit = $perms->checkModule('roles', 'edit');
$canDelete = $perms->checkModule('roles', 'delete');

$crole =& new CRole;
$roles = $crole->getRoles();

$role_id = dPgetParam( $_GET, 'role_id', 0 );

$modules = 
$sql = 'SELECT mod_id, mod_name FROM modules WHERE mod_active > 0 ORDER BY mod_directory';
$modules = arrayMerge( array( '0' => 'All' ), db_loadHashList( $sql ) );

// setup the title block
$titleBlock = new CTitleBlock( 'Roles settings', 'roles-settings.png' );
$titleBlock->addButton('Main page', '?m=system');
$titleBlock->show();
?>

<script language="javascript">
<? if ($canDelete) { ?>
function doDeleteRole(id) {
	if (confirm( 'Are you sure you want to delete this?' )) {
		f = document.roleFrm;
		f.del.value = 1;
		f.role_id.value = id;
		f.submit();
	}
}
<? } ?>

<? if ($canEdit) { ?>
function doEditRole(id) {
	var r = $('idRole'+id);
	var f = document.roleFrm;

	f.role_id.value = id;
	f.role_name.value = r.cells[2].innerHTML;
	f.role_description.value = r.cells[3].innerHTML;
	Dialog('idRoleEdit').show();
}
<? } ?>
</script>

<table border="0" cellpadding="2" cellspacing="1" width="100%" class="list">
<tr>
	<th colspan="2">&nbsp;</th>
	<th><?=$AppUI->_('Role ID')?></th>
	<th><?=$AppUI->_('Description')?></th>
	<th>&nbsp;</th>
</tr>
<?php

function showRow( $role=null ) {
	global $canEdit, $canDelete, $AppUI, $modules;
	if ($role == null)
		$id = 0;
	else {
		$id = $role['id'];
		$name = $role['value'];
		$description = $role['name'];
	}

	$s = '<tr id="idRole'.$id.'" class="second">';
	if ($id == 0 && $canEdit) {
		$s .= '<td colspan="2" align="center">';
		$s .= '<img class="clickable" src="images/add_log.gif" onclick="doEditRole(0)" title="Add"/>';
		$s .= '</td><td></td><td></td><td></td>';
	} else {
		$s .= '<td width="12">';
		if ($canEdit) {
			$s .= '<a href="?m=system&u=roles&a=viewrole&role_id='.$id.'&tab=1" title="Adjust permission">';
			$s .= dPshowImage('images/role.gif');
			$s .= '</a></td>';
			$s .= '<td width="12"><a href="javascript:doEditRole('.$id.')" title="Edit">';
			$s .= dPshowImage('images/edit.gif');
			$s .= '</a>';
		}
		$s .= '</td>';
		$s .= '<td>'.$name.'</td>';
		$s .= '<td>'.$AppUI->_($description).'</td>';
		$s .= '<td width="16">';
		if ($canDelete) {
			$s .= '<a href="javascript:doDeleteRole('.$id.')">';
			$s .= dPshowImage('images/common_delete.gif');
			$s .= '</a>';
		}
		else {
			$s .= '<img src="images/blank.gif" width="12" height="12">';
		}
		$s .= '</td>';
	}
	$s .= '</tr>';
	return $s;
}

// do the modules that are installed on the system
$s = '';
foreach ($roles as $row) {
	echo showRow( $row );
}
// add in the new key row:
if ($role_id == 0) {
	echo showRow();
}
?>
</table>
<div id="idRoleEdit" class="popup" style="visibility: hidden" autohide="click">
<form name="roleFrm" method="post" action="?m=system&u=roles">
<input type="hidden" name="dosql" value="do_role_aed" />
<input type="hidden" name="del" value="0" />
<input type="hidden" name="role_id" value="0" />
<table border="0" cellspacing="0" cellpadding="8" width="200px">
<tr>
	<th colspan="2" bgcolor="#E0E0E0"><?=$AppUI->_('Role information')?></th>
</tr>
<tr>
<tr>
	<td align="right"><?=$AppUI->_('Role ID')?></td>
	<td><input type="text" name="role_name" class="text" value=""/></td>
</tr>
<tr>
	<td align="right"><?=$AppUI->_('Description')?></td>
	<td><input type="text" name="role_description" class="text" value=""></td>
</tr>
<tr>
	<td colspan="2" align="center">
		<input type="submit" value="<?=$AppUI->_('Save')?>"/>
		<input type="button" value="<?=$AppUI->_('Close')?>" onclick="Dialog('idRoleEdit').hide()"/>
	</td>
</tr>
</table>
</form>
</div>
